package org.wuqi.microweb.api.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import org.apache.dubbo.common.logger.Logger;
import org.apache.dubbo.common.logger.LoggerFactory;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundSetOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.wuqi.api.model.BaseInfo;
import org.wuqi.api.model.BaseInfoIndex;
import org.wuqi.api.model.ProductDetail;
import org.wuqi.api.service.PlatBaseInfo;
import org.wuqi.api.service.PlatIndexInfo;
import org.wuqi.api.service.ProductInfo;
import org.wuqi.bean.LoanInfo;
import org.wuqi.microweb.api.result.BaseInfoResult;
import org.wuqi.microweb.api.result.Result;
import org.wuqi.microweb.api.result.ResultEnum;
import org.wuqi.microweb.api.testmethod.SyncThread4;
import org.wuqi.microweb.api.testmethod.TTest;

import java.io.IOException;
import java.net.http.HttpClient;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CountDownLatch;

/**
 * @author jjj
 * @program: earnest-money
 * @description:TODO
 * @date 2023-12-19 11:48:19
 */
@RestController
@RequestMapping("/v1")
@Validated
public class BaseInfoContorller {
    private Logger logger = LoggerFactory.getLogger(BaseInfoContorller.class);
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @DubboReference(interfaceClass = PlatBaseInfo.class, version = "1")
    PlatBaseInfo platBaseInfo;
    @DubboReference(interfaceClass = ProductInfo.class, version = "1")
    ProductInfo productInfo;
    @DubboReference(interfaceClass = PlatIndexInfo.class, version = "1")
    PlatIndexInfo platIndexInfo;

    @GetMapping("/getBaseInfo")
    public BaseInfoResult getBaseInfo() {
        BaseInfo baseInfo = platBaseInfo.queryPlatBaseInfo();
        Map<String, Object> responseMap = new HashMap<>();
        responseMap.put("user", baseInfo);
        BaseInfoResult baseResult = new BaseInfoResult("成功", "200", responseMap);
        return baseResult;
    }
    @GetMapping("/getProductList")
    public BaseInfoResult getProductList(@RequestParam("pageSize") Integer pageSize, @RequestParam("pageNum") Integer pageNum) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("pageSize", pageSize);
        map.put("pageNum", pageNum);
        PageHelper.startPage(pageNum, pageSize);
        List<LoanInfo> loanInfos = productInfo.selectProductList(map);
        PageInfo pageInfo = new PageInfo(loanInfos);
        Map<String, Object> responseMap = new HashMap<>();
        responseMap.put("user", pageInfo);
        BaseInfoResult baseResult = new BaseInfoResult("成功", "200", responseMap);
        return baseResult;
    }
    @GetMapping("/rank")
    public void showInvestRank() {
        System.out.println("=====");
        stringRedisTemplate.opsForSet().add("data-set", "java");
        Set<String> members = stringRedisTemplate.opsForSet().members("data-set");
//       String pop = stringRedisTemplate.opsForSet().pop("data-set");
        System.out.println(members);
        members.forEach(val -> System.out.println(val));
    }
    //   首页的数据查询
    @GetMapping("/baseindex")
    public BaseInfoResult showIndexPage() {
        BaseInfoIndex baseInfoIndex = platIndexInfo.queryIndexInfo();
        Map<String, Object> responseMap = new HashMap<>();
        responseMap.put("data", baseInfoIndex);
        BaseInfoResult baseInfoResult = new BaseInfoResult("成功", "200", responseMap);
        return baseInfoResult;
    }
    @GetMapping("/getProductDetail")
    public BaseInfoResult getProductDetail(@NotBlank(message = "id不能为空") @Min(value = 1, message = "id要大于等于0") String id) {
        Integer i = Integer.valueOf(id);
        ProductDetail productDetail = productInfo.selectDetail(i);
        Map<String, Object> responseMap = new HashMap<>();
        responseMap.put("data", productDetail);
        BaseInfoResult baseInfoResult = new BaseInfoResult("成功", "200", responseMap);
        return baseInfoResult;
    }
    @GetMapping("/getMsg")
    public void getMsg() throws IOException {
        // 创建Httpclient对象
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpGet httpGet = new HttpGet("https://www.baidu.com");
        CloseableHttpResponse response = null;
        try {
            response = httpclient.execute(httpGet);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            httpclient.close();
        }
    }
    @GetMapping("/test")
    public void test() throws IOException, InterruptedException {
       /* SyncThread syncThread = new SyncThread();
        Thread thread = new Thread(syncThread);
        thread.start();*/
       /* SyncThread2 syncThread2 = new SyncThread2();
        syncThread2.run();*/
//        SyncThread3 syncThread4 = new SyncThread3();
        /*
        syncThread3.doTask1();
        syncThread3.doTask2();*/
        /*syncThread4.doTask1();
        syncThread4.doTask2();*/
//        CountDownLatch latch = new CountDownLatch(3);
        CountDownLatch latch = new CountDownLatch(3);
        Runnable task = () -> {
            System.out.println("Task started");
            // 模拟耗时操作
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("Task completed");
            latch.countDown(); // 操作完成后计数减一
        };
        // 启动多个线程执行任务
        new Thread(task).start();
        new Thread(task).start();
        new Thread(task).start();
        // 等待所有任务完成
        latch.await(); // 等待计数器为零
        System.out.println("All tasks completed");
        logger.info("===== 主线程 =====");
    }
    @GetMapping("/send")
    public Result<String> send() {
        System.out.println("====");
    /*    SyncThread4 syncThread4 = new SyncThread4();
        Thread thread = new Thread(syncThread4);
        Thread thread1 = new Thread(syncThread4);
        thread.start();
        thread1.start();*/
        System.out.println("枚举示例"+ ResultEnum.SUCCESS);
        System.out.println("枚举示例"+ ResultEnum.SUCCESS.getCode());
        return Result.success("我是成功信息");
    }

}